(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:MemberlevelCtrl
     * @description
     * # MemberlevelCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('MemberlevelCtrl', MemberlevelCtrl);

    MemberlevelCtrl.$inject = ['$scope', 'dataService', 'helper', '$state', '$location', 'sessionService'];

    function MemberlevelCtrl($scope, dataService, helper, $state, $location, sessionService) {

        $scope.listAction = function() {
            $scope.pageSize = 10;
            $scope.getlist(1);
        }

        $scope.getlist = function(pageNo, member_level_id) {
            var query = {
                page_no: pageNo ? pageNo : "1",
                page_size: $scope.pageSize
            };
            if (member_level_id) {
                query.query = {
                    member_level_id: member_level_id
                };
            }

            dataService.memberlevelGet(query).then(function(list) {
                if (list.member_levels) {
                    if (member_level_id) {
                        $scope.member_level = list.member_levels[0];
                    } else {
                        $scope.member_levels = list.member_levels;
                        $scope.total_count = list.total_count;
                    }
                }
            });
        }

        $scope.addAction = function() {
            $scope.member_level_id = $location.search().member_level_id;
            if ($scope.member_level_id) {
                $scope.getlist(1, $scope.member_level_id);
            }
        }

        $scope.addData = function() {
            var apiAry = {
                "add": "memberlevelAdd",
                "up": "memberlevelUpdate",
            }

            var api = "add";
            if ($scope.member_level_id) {
                api = "up";
            }

            $scope.member_level_id = $location.search().member_level_id;
            var query = {};
            query.query = {
                "partner_id": sessionService.get('tokenUser').CompanyId,
                "value": $scope.member_level.value
            };

            dataService.memberlevelGet(query).then(function(list) {
                if (!list.member_levels.length) {
                    if ($scope.member_level.require_exp == 0) {
                        helper.myAlert("会员等级积分，不能为0");
                        return false;
                    }
                    dataService[apiAry[api]]($scope.member_level).then(function(re2) {
                        if (re2.Msg) {
                            helper.myAlert("error:" + re2.Msg);
                            return false;
                        } else {
                            helper.myAlert("success", null, null, function() {
                                $state.go("memberlevel.list");
                            });
                        }
                    });
                } else {
                    if (!$scope.member_level_id && list.member_levels[0] && list.member_levels[0].value == $scope.member_level.value) {
                        helper.myAlert("相同等级值不能重复创建！");
                        return false;
                    } else {
                        //积分不能设置0
                        if ($scope.member_level.require_exp == 0) {
                            helper.myAlert("会员等级积分，不能为0");
                            return false;
                        }

                        dataService[apiAry[api]]($scope.member_level).then(function(re2) {
                            if (re2.Msg) {
                                helper.myAlert("error:" + re2.Msg);
                                return false;
                            } else {
                                helper.myAlert("success", null, null, function() {
                                    $state.go("memberlevel.list");
                                });
                            }
                        });
                    }
                }
            });
        }
    }
})();